﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

using LEEAAttendanceControl.Web;
using LEEAAttendanceControl.Web.Services;

using System.ServiceModel.DomainServices.Client;
using LEEAAttendanceControl.General;

namespace LEEAAttendanceControl.Views
{
    public partial class EmployeesExceptions : Page
    {
        LEEAContext context = new LEEAContext();

        public EmployeesExceptions()
        {
            InitializeComponent();
        }

        void ChangeEmployees()
        {
            ExAssignmentsDataSource.QueryParameters.Clear();

            App app = (App)Application.Current;

            Parameter pc = new Parameter();

            pc.ParameterName = "companyid";
            pc.Value = app.CompanyID.HasValue ? app.CompanyID.Value : No.Zero;
            ExAssignmentsDataSource.QueryParameters.Add(pc);

            Parameter pd = new Parameter();

            pd.ParameterName = "departmentid";
            pd.Value = app.DepartmentID.HasValue ? app.DepartmentID.Value : No.Zero;
            ExAssignmentsDataSource.QueryParameters.Add(pd);

            Parameter pe = new Parameter();

            pe.ParameterName = "employeeid";
            pe.Value = app.EmployeeId;
            ExAssignmentsDataSource.QueryParameters.Add(pe);

            ExAssignmentsDataSource.Load();
        }
       
        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            App myApp = (App)Application.Current;

            myApp.ClearEvents();

            myApp.EditEvent += EditEmployeeExceptions;
            myApp.DeleteEvent += DeleteEmployeeExceptions;

            myApp.ChangeEmployeesEvent += ChangeEmployeesHandler;

            ChangeEmployees();
        }

        public void ChangeEmployeesHandler(object sender, EventArgs e)
        {
            ChangeEmployees();
        }

        private void DomainDataSource_LoadingData(object sender, LoadingDataEventArgs e)
        {
            e.LoadBehavior = LoadBehavior.RefreshCurrent;
        }

        public void EditEmployeeExceptions(object sender, EventArgs e)
        {
            ExceptionAssignmentSummary summary = ExAssignmentsGrid.SelectedItem as ExceptionAssignmentSummary;

            ExceptionDays days = ExceptionsGrid.SelectedItem as ExceptionDays;

            if (summary != null && days != null)
            {
                ExceptionAssignmentEdit exAssignEdit = new ExceptionAssignmentEdit();

                exAssignEdit.LoadData(summary.EmployeeID, days.AssignmentID);
                exAssignEdit.Closed += ExceptionAssignmentWindow_Closed;

                exAssignEdit.Show();
            }
        }

        private void ExceptionAssignmentWindow_Closed(object sender, EventArgs e)
        {
            ReloadExceptionsAssignments();
        }

        public void DeleteEmployeeExceptions(object sender, EventArgs e)
        {
            ExceptionDays exAssignment = ExceptionsGrid.SelectedItem as ExceptionDays;

            if (exAssignment != null)
            {
                MessageDisplay message = new MessageDisplay(ApplicationStrings.DeleteMessage
                    , ApplicationStrings.Delete, "Ask");

                message.Closed += (s, ee) =>
                {
                    if (message.DialogResult.Value)
                    {
                        context.DestroyExceptionAssignmentByID(exAssignment.AssignmentID
                            , OnExAssignmentDeleteCompleted, null);
                    }
                };

                message.Show();
            }
        }

        private void OnExAssignmentDeleteCompleted(InvokeOperation io)
        {
            if (!io.HasError)
            {
                ReloadExceptionsAssignments();
            }
        }

        private void ExAssignmentsGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ReloadExceptionsAssignments();
        }

        private void ReloadExceptionsAssignments()
        {
            ExceptionAssignmentSummary summary = ExAssignmentsGrid.SelectedItem as ExceptionAssignmentSummary;

            if (summary != null)
            {
                ExceptionsGrid.ItemsSource = null;

                context.ExceptionDays.Clear();

                context.Load(context.GetExceptionDaysQuery(summary.EmployeeID, ((App)Application.Current).PeriodFrom.Date
                    , ((App)Application.Current).PeriodTo.Date), OnExceptionsLoadCompleted, null);
            }
        }

        private void OnExceptionsLoadCompleted(LoadOperation load)
        {
            ExceptionsGrid.ItemsSource = load.Entities;
        }
    }
}
